/*
 * This file is part of the Sx Framework Library.
 * 
 * Copyright (C) 2013 University of Colorado Denver
 * <min.choi@ucdenver.edu> <shane.transue@ucdenver.edu>
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy 
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
 * copies of the Software, and to permit persons to whom the Software is 
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in 
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
 * DEALINGS IN THE SOFTWARE.
 */
#include <sxAbstractSimulation.h>
#include <sxSimulationListener.h>

/* Default Simulation Time-step: 30[Hz] */
const double Sx::Simulation::AbstractSimulation::DEFAULT_TIMESTEP = 1.0 / 30.0;
const Sx::Simulation::TimeStepType Sx::Simulation::AbstractSimulation::DEFAULT_TIMESTEP_TYPE = Sx::Simulation::REALTIME;

Sx::Simulation::AbstractSimulation::AbstractSimulation() {
	this->timeStep = DEFAULT_TIMESTEP;
	this->timeStepType = DEFAULT_TIMESTEP_TYPE;
	this->paused = false;
	this->currentTimeStep = 0u;
}

Sx::Simulation::AbstractSimulation::~AbstractSimulation() {
	this->listeners.clear();
}

bool Sx::Simulation::AbstractSimulation::construct() {
	for ( unsigned int i = 0; i < this->listeners.size(); i++ ) {
		if ( this->listeners[i] == nullptr ) continue;
		this->listeners[i]->onConstruct();
	}

	return true;
}

void Sx::Simulation::AbstractSimulation::preUpdate() {
	for ( unsigned int i = 0; i < this->listeners.size(); i++ ) {
		if ( this->listeners[i] == nullptr ) continue;
		this->listeners[i]->preUpdate();
	}
}

void Sx::Simulation::AbstractSimulation::postUpdate() {
	for ( unsigned int i = 0; i < this->listeners.size(); i++ ) {
		if ( this->listeners[i] == nullptr ) continue;
		this->listeners[i]->postUpdate();
	}

	this->currentTimeStep++;
}

void Sx::Simulation::AbstractSimulation::preRender() const {
	for ( unsigned int i = 0; i < this->listeners.size(); i++ ) {
		if ( this->listeners[i] == nullptr ) continue;
		this->listeners[i]->preRender();
	}
}

void Sx::Simulation::AbstractSimulation::postRender() const {
	for ( unsigned int i = 0; i < this->listeners.size(); i++ ) {
		if ( this->listeners[i] == nullptr ) continue;
		this->listeners[i]->postRender();
	}
}

void Sx::Simulation::AbstractSimulation::reset() {
	for ( unsigned int i = 0; i < this->listeners.size(); i++ ) {
		if ( this->listeners[i] == nullptr ) continue;
		this->listeners[i]->onReset();
	}
	this->currentTimeStep = 0;
}

void Sx::Simulation::AbstractSimulation::pause() {
	this->paused = true;

	for ( unsigned int i = 0; i < this->listeners.size(); i++ ) {
		if ( this->listeners[i] == nullptr ) continue;
		this->listeners[i]->onPause();
	}
}

void Sx::Simulation::AbstractSimulation::resume() {
	this->paused = false;

	for ( unsigned int i = 0; i < this->listeners.size(); i++ ) {
		if ( this->listeners[i] == nullptr ) continue;
		this->listeners[i]->onResume();
	}
}

void Sx::Simulation::AbstractSimulation::setPaused(bool pause) {
	this->paused = pause;
}

void Sx::Simulation::AbstractSimulation::setTimeStep(double timeStep) {
	this->timeStep = timeStep;
}

void Sx::Simulation::AbstractSimulation::setTimeStepType(TimeStepType type) {
	this->timeStepType = type;
}

bool Sx::Simulation::AbstractSimulation::isPaused() const {
	return this->paused;
}

double Sx::Simulation::AbstractSimulation::getTimeStep() const {
	return this->timeStep;
}

Sx::Simulation::TimeStepType Sx::Simulation::AbstractSimulation::getTimeStepType() const {
	return this->timeStepType;
}

bool Sx::Simulation::AbstractSimulation::addListener(SimulationListener* listener) {
	if ( listener == nullptr ) {
		std::cerr << "[AbstractSimulation:addListener] Error: Cannot add nullptr listener to abstract simulation." << std::endl;
		return false;
	}

	this->listeners.add(listener);
	return true;
}

bool Sx::Simulation::AbstractSimulation::removeListener(SimulationListener* listener) {
	if ( listener == nullptr ) {
		std::cerr << "[AbstractSimulation:removeListener] Error: Cannot remove nullptr listener from abstract simulation." << std::endl;
		return false;
	}

	this->listeners.remove(listener);
	return true;
}
